package com.hn.utils.http;

import com.hn.Entry.ServerEntry;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.utils.ExternalResource;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ImageUtil {
    public static String getImage(String path, MessageEvent event){
        File file=new File(path);
        ExternalResource externalResource = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            externalResource = ExternalResource.create(fileInputStream);
            Contact contact = event.getSubject();
            Image image = contact.uploadImage(externalResource);
            return image.getImageId();
        } catch (IOException e) {
            return null;
        }finally {
            if (externalResource != null) {
                try {
                    externalResource.close();
                } catch (IOException e) {
                    System.out.println("本地图片获取失败"+e.getMessage());
                }
            }
        }
    }
    public static File imgBuilder(List<ServerEntry> serverEntries) {
        try {
            // 加载背景图片
            File backgroundFile = new File("D:\\Bot\\serverQueryBg.png");
            BufferedImage backgroundImage = ImageIO.read(backgroundFile);

            // 创建一个新的图片，大小与背景图片相同
            BufferedImage combinedImage = new BufferedImage(backgroundImage.getWidth(), backgroundImage.getHeight(), BufferedImage.TYPE_INT_ARGB);

            Graphics2D g2d = combinedImage.createGraphics();
            // 绘制背景图片
            g2d.drawImage(backgroundImage, 0, 0, null);
            // 绘制生成时间
            g2d.setColor(Color.RED);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 32));
            // 获取当前日期和时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日 HH:mm:ss");
            g2d.drawString(sdf.format(new Date()), backgroundImage.getWidth()-1170, backgroundImage.getHeight()-20);
            //绘制网站连接
            g2d.setColor(Color.RED);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 32));
            g2d.drawString("网页版登录器地址 : https://www.bluearchive.top/", backgroundImage.getWidth()-700, backgroundImage.getHeight()-20);

            // 设置盒子参数
            int boxWidth =(backgroundImage.getWidth()/4)-13;
            int boxHeight = (backgroundImage.getHeight()/4)-30;
            int margin = 10;
            // 计算需要多少行来容纳所有盒子
            int rows = (serverEntries.size() + 3) / 4;

            // 绘制圆角盒子
            Graphics2D roundedG2d = (Graphics2D) g2d.create();
            roundedG2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Color semiTransparentGray = new Color(0, 0, 0, 160);

            // 绘制圆角盒子和文字
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < 4 && row * 4 + col < serverEntries.size(); col++) {
                    int x = margin + col * (boxWidth + margin);
                    int y = margin + row * (boxHeight + margin);

                    //添加盒子
                    RoundRectangle2D.Double roundRect = new RoundRectangle2D.Double(x, y, boxWidth, boxHeight, 20, 20);
                    roundedG2d.setColor(semiTransparentGray); // 设置圆角矩形的颜色
                    roundedG2d.fill(roundRect);

                    //给盒子里添加文字
                    ServerEntry serverEntry = serverEntries.get(row * 4 + col);
                    roundedG2d.setColor(Color.WHITE);
                    roundedG2d.setFont(new Font("微软雅黑", Font.BOLD, 38));
                    String serverName = "服务器名称:"+serverEntry.getName();
                    roundedG2d.drawString(serverName, x + 10, y + 50);
                    String players="("+serverEntry.getPlayers()+"/"+serverEntry.getMax_players()+")";
                    roundedG2d.drawString(players,x+630,y+50);
                    String map="地图名:"+serverEntry.getMap();
                    roundedG2d.drawString(map, x+10,y+120);
                    String label="地图译名:"+serverEntry.getMapName();
                    roundedG2d.drawString(label, x+10,y+190);
                    // 加载网络图片
                    try {
                        URL imageUrl = new URL(serverEntries.get(row * 4 + col).getMapImg()); // 假设serverEntry有一个getMapImg()方法返回图片URL
                        int imgWidth=boxWidth ;
                        int imgHeight = boxHeight / 2 + 20; // 图片高度为盒子宽度的一b半
                        BufferedImage mapImage = resizeImage(imageUrl, imgWidth/2, imgHeight-20);
                        // 绘制图片
                        g2d.drawImage(mapImage, x+10, y+220, null); // 图片绘制在盒子右侧
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.err.println("加载图片时发生错误：" + e.getMessage());
                    }
                }
            }

            roundedG2d.dispose();

            // 保存生成的图片
            File outputFile = new File("D:\\Bot\\background_with_boxes.png");
            ImageIO.write(combinedImage, "png", outputFile);

            return outputFile;

        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("处理图片时发生错误。");
        }
        return null;
    }

    // 辅助方法：从URL下载并调整图片大小
    public static BufferedImage resizeImage(URL imageUrl, int targetWidth, int targetHeight) throws IOException {
        BufferedImage originalImage = ImageIO.read(imageUrl);
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = resizedImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.drawImage(originalImage.getScaledInstance(targetWidth, targetHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        g2d.dispose();
        return resizedImage;
    }
}
